package hasthi.metamodel.resource;

import static hasthi.wsdm.capabilities.MetricsCapability.*;
import static hasthi.wsdm.capabilities.MetricsCapability.LAST_REQUEST_RECIVED_TIME;
import static hasthi.wsdm.capabilities.MetricsCapability.MAX_RESPONSE_TIME;
import static hasthi.wsdm.capabilities.MetricsCapability.NUMBER_OF_SUCCESSFUL_REQUESTS;
import static hasthi.wsdm.capabilities.MetricsCapability.PENDING_REQUESTS;
import hasthi.common.HasthiException;
import hasthi.common.constants.WSDMConstants;
import hasthi.common.constants.WSDMConstants.SystemState;
import hasthi.manager.ManagedResourceType;
import hasthi.wsdm.ManagedServiceHandle;

import java.util.Collection;

import javax.xml.namespace.QName;

public class ManagedService implements EditableManagedResource {
    protected EditableManagedResource rawResource;

    public ManagedService(EditableManagedResource rawResource) throws HasthiException {
        this.rawResource = rawResource;
    }

    public String getCategory() {
        return rawResource.getCategory();
    }

    public String getManagerAgentURL() {
        return rawResource.getManagerAgentURL();
    }

    public QName getQName() {
        return rawResource.getQName();
    }

    public Collection<String> getProperties() {
        return rawResource.getProperties();
    }

    public String getProperty(String name) throws HasthiException {
        return rawResource.getProperty(name);
    }

    public String getState() throws HasthiException {
        return rawResource.getState();
    }

    public String getType() {
        return rawResource.getType();
    }

    public ManagedResourceType toXml(boolean summerized) throws HasthiException {
        return rawResource.toXml(summerized);
    }

    public String getHost() throws HasthiException {
        return (String) getProperty(WSDMConstants.HOSTNAME_PARAM.getLocalPart());
    }

    public long getLastUpdated() {
        return rawResource.getLastUpdated();
    }
    
    public int getPendingRequests()throws HasthiException{
        return getIntProperty(PENDING_REQUESTS.getLocalPart());
    }
    
    public int getFailedRequests()throws HasthiException{
        return getIntProperty(FALIED_REQUESTS.getLocalPart());
    }
    
    public int maxResponseTime()throws HasthiException{
        return getIntProperty(MAX_RESPONSE_TIME.getLocalPart());
    }
    
    public int getSucessfulRequests()throws HasthiException{
        return getIntProperty(NUMBER_OF_SUCCESSFUL_REQUESTS.getLocalPart());
    }
    
    public long getLastRequestTime()throws HasthiException{
        return getLongProperty(LAST_REQUEST_RECIVED_TIME.getLocalPart());
    }
    
    public double getServiceMemeoryUsagePercent() throws HasthiException{
        return getDoubleProperty(ManagedServiceHandle.SERVICE_MEMEORY_USAGE.getLocalPart());
    }
    
    public double getFileDescUsagePercent()throws HasthiException{
        return getDoubleProperty(ManagedServiceHandle.SERVICE_FILEDESC_USAGE.getLocalPart());
    }

    public double getThreadCount()throws HasthiException{
        return getIntProperty(ManagedServiceHandle.SERVICE_THERAD_COUNT.getLocalPart());
    }

    public int getIntProperty(String name) throws HasthiException{
        String value = (String)rawResource.getProperty(name);
        if(value == null){
            return -1;
        }else{
            return Integer.parseInt(value);
        }
    }
    
    public double getDoubleProperty(String name) throws HasthiException{
        String value = (String)rawResource.getProperty(name);
        if(value == null){
            return -1;
        }else{
            return Double.parseDouble(value);
        }
    }
    
    public long getLongProperty(String name) throws HasthiException{
        String value = (String)rawResource.getProperty(name);
        if(value == null){
            return -1;
        }else{
            return Long.parseLong(value);
        }
    }

    public String getName() {
       return rawResource.getName();
    }

    @Override
    public String toString() {
        return rawResource.toString();
    }

    public void setLastUpdated(long lastUpdated) {
        rawResource.setLastUpdated(lastUpdated);
    }

    public void setProperty(String name, Object value) throws HasthiException {
        rawResource.setProperty(name, value);
    }

    public void setState(SystemState state) throws HasthiException {
        rawResource.setState(state);
    }

    public EditableManagedResource getRawResource() {
        return rawResource;
    }

    public void setRawResource(EditableManagedResource rawResource) {
        this.rawResource = rawResource;
    }

    public String getGroup()throws HasthiException {
        return rawResource.getGroup();
    }
    public long getServiceStartTime() throws HasthiException{
        return getLongProperty(SERVICE_START_TIME.getLocalPart());
    }

    public boolean isActive() {
        return rawResource.isActive();
    }

    public void setActive(boolean active) {
        this.rawResource.setActive(active);
    }

    @Override
    public boolean equals(Object obj) {
        return rawResource.equals(obj);
    }

    @Override
    public int hashCode() {
        return rawResource.hashCode();
    }
   
}
